<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>登录</title>
  <link rel="stylesheet" href="./bootstrap/bootstrap.min.css" />
  <link rel="stylesheet" href="./bootstrap/bootstrap-icons.css" />
  <link rel="stylesheet" href="./css/common.css" />
  <link rel="stylesheet" href="./css/login.css" />
</head>

<body class="application application-offset">
  <div class="container-fluid container-application">
    <div class="main-content position-relative">
      <!-- Page content -->
      <div class="page-content">
        <div class="min-vh-100 py-5 d-flex align-items-center">
          <div class="w-100">
            <div class="row justify-content-center">
              <div class="col-sm-8 col-lg-4">
                <div class="card shadow zindex-100 mb-0">
                  <div class="card-body px-md-5 py-5">
                    <div class="mb-5">
                      <h6 class="h3">传智教育-登录</h6>
                    </div>
                    <span class="clearfix"></span>
                    <form role="form" autocomplete="off">
                      <div class="form-group">
                        <label class="form-control-label">账号</label>
                        <div class="input-group input-group-merge">
                          <div class="input-group-prepend">
                            <span class="input-group-text"><i class="bi bi-person"></i></span>
                          </div>
                          <input name="username" type="text" class="form-control" id="input-email"
                            placeholder="请输入账号" />
                        </div>
                      </div>
                      <div class="form-group mb-4">
                        <div class="d-flex align-items-center justify-content-between">
                          <div>
                            <label class="form-control-label">密码</label>
                          </div>
                          <div class="mb-2">
                            <a href="javascript:;"
                              class="small text-muted text-underline--dashed border-primary">忘记密码</a>
                          </div>
                        </div>
                        <div class="input-group input-group-merge">
                          <div class="input-group-prepend">
                            <span class="input-group-text"><i class="bi bi-key"></i></span>
                          </div>
                          <input name="password" type="password" class="form-control" id="input-password"
                            placeholder="请输入密码" />
                          <div class="input-group-append">
                            <span class="input-group-text">
                              <a href="javascript:;" data-toggle="password-text" data-target="#input-password">
                                <i class="bi bi-eye-fill text-blue"></i>
                              </a>
                            </span>
                          </div>
                        </div>
                      </div>
                      <div class="mt-4">
                        <button id="btn-login" type="button" class="btn btn-sm btn-info btn-icon btn-blue rounded-pill">
                          <span class="btn-inner--text">登录</span>
                          <span class="btn-inner--icon"><i class="bi bi-arrow-right"></i></span>
                        </button>
                      </div>
                    </form>
                  </div>
                  <div class="card-footer px-md-5">
                    <small>没有注册？</small>
                    <a href="./register.html" class="small font-weight-bold text-blue">创建账号</a>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- toast -->
  <div class="position-fixed top-0 start-50 pt-4" style="z-index: 999; transform: translateX(-50%)">
    <div id="myToast" class="toast bg-rgba">
      <div class="toast-body">提示消息</div>
    </div>
  </div>
  <script src="./bootstrap/bootstrap.min.js"></script>
  <script src="./lib/form-value.js"></script>
  <script src="./lib/axios.js"></script>
  <script src="./lib/message.js"></script>
  <script src="./js/common.js"></script>
  <script>
    // 注册业务：获取输入框的账号，密码提交黑接口即可
    // 如果有问题,则提示一下(如果账号已经村子,比如账号太长了)
    // 如果注册成功,则跳转到登录页去登录

    // 找到按钮注册点击事件(注册默认行为)
    document.querySelector('#btn-login').addEventListener('click', function (e) {
      e.preventDefault()
      // let username = 第一个输入框.value
      // let password = 第一个输入框.value
      // 使用插件收集表单各项的值(form-value查件(val))
      // let data = val(表单)//val()函数会把表单各项的值收集到 (要求表单各项必须有name的属性)(打印检查)

      // Ajax把数据提交给接口(看文档)
      let data = val(document.querySelector('form'))

      // 简单验证一下数据格式
      if (data.username.length < 2 || data.username.length > 30) {
        alert('用户名长度应该是2~30位')
        return
      }
      if (data.password.length < 6 || data.password.length > 30) {
        alert('密码长度应该是6~30位')
        return
      }
      axios({
        url: 'http://ajax-api.itheima.net/login',
        method: 'post',
        data: data,
      }).then(res => {
        // console.log(res.data.message)
        // 成功后提示
        localStorage.setItem('token', res.data.data.token)
        message.success(res.data.message)//提示注测成功
        // 清空输入框的值
        document.querySelector('form').reset()
        // 跳转到登录页
        setInterval(() => {
          // 等一下，等提示隐藏了再跳转
          location.href = './index.html'
        }, 1500)
      }).catch(err => {
        // console.dir(err)
        alert(err.response.data.message)//提示账号已经存在
      })


    })


  </script>
</body>

</html>